﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.MarketUI {
    import Classes.*;
    import ZLocalization.*;
    import Modules.franchise.data.*;

    public class BuyLogic {

        public static function friendLevelCheck(_arg1:Item):Boolean{
            var _local2:Friend;
            var _local3:uint;
            var _local4:uint;
            if (Global.isVisiting()){
                for each (_local2 in Global.friendbar) {
                    if (_local2.m_uid == Global.world.ownerId){
                        _local4 = _local2.m_level;
                        break;
                    };
                };
                _local3 = _arg1.requiredLevel;
                if (_local3 > _local4){
                    return (false);
                };
            };
            return (true);
        }
        public static function franchiseCheck(_arg1:Item):Boolean{
            var _local3:Item;
            var _local4:MapResource;
            var _local2:Array = Global.world.getObjectsByTypes([WorldObjectTypes.CONSTRUCTION_SITE, WorldObjectTypes.BUSINESS]);
            for each (_local4 in _local2) {
                _local3 = Global.gameSettings.getItemByName(Item.findUpgradeRoot(_local4.getItem()));
                if (_local4.itemOwner == Global.player.uid){
                    if (_local3.name == _arg1.name){
                        return (false);
                    };
                    if ((((((_local4 is ConstructionSite)) && (((_local4 as ConstructionSite).targetClass == Business)))) && (((_local4 as ConstructionSite).targetName == _arg1.name)))){
                        return (false);
                    };
                };
            };
            return (true);
        }
        public static function lotSiteCheck(_arg1:Item):Boolean{
            if (_arg1.isEmptyLot){
                return (!(maxReceivedFranchisesCheckDefault()));
            };
            return (true);
        }
        public static function canFranchise(_arg1:Item):Boolean{
            return (_arg1.canFranchise);
        }
        public static function hasFranchiseCheck(_arg1:Item):Boolean{
            var _local2:OwnedFranchiseData = Global.franchiseManager.model.getOwnedFranchise(_arg1.name);
            if (_local2){
                return (true);
            };
            return (false);
        }
        public static function franchiseCountLevelCheck(_arg1:Item):Boolean{
            var _local2:Boolean = Global.franchiseManager.model.getOwnsFranchise(_arg1.name);
            var _local3:Boolean = ((_local2) && ((Global.franchiseManager.getFranchise(_arg1.name).getLocationCount() > 0)));
            if (_local3){
                return (true);
            };
            return (!(Global.franchiseManager.isFranchiseLocked()));
        }
        public static function getFranchisesCount(_arg1:String):int{
            var _local4:Business;
            var _local5:Array;
            var _local2:Array = Global.world.getObjectsByTypes([WorldObjectTypes.BUSINESS]);
            var _local3:int;
            for each (_local4 in _local2) {
                if (_local4.itemOwner != _arg1){
                    _local3++;
                };
            };
            _local5 = Global.world.getObjectsByTypes([WorldObjectTypes.LOT_SITE]);
            _local3 = (_local3 + _local5.length);
            return (_local3);
        }
        public static function canAcceptFranchiseRequests(_arg1:String):Boolean{
            var _local4:Business;
            var _local2:Array = Global.world.getObjectsByTypes([WorldObjectTypes.BUSINESS]);
            var _local3:int;
            for each (_local4 in _local2) {
                if (_local4.itemOwner != _arg1){
                    _local3++;
                };
            };
            if (_local3 < maxFranchiseCount){
                return (true);
            };
            return (false);
        }
        public static function maxReceivedFranchisesCheck(_arg1:Item):Boolean{
            if (_arg1.className == "LotSite"){
                return (maxReceivedFranchisesCheckDefault());
            };
            return (false);
        }
        public static function maxReceivedFranchisesCheckDefault():Boolean{
            var _local2:int;
            var _local1:int = getFranchisesCount(Global.player.uid);
            _local2 = maxAllowedFranchiseCount;
            if (_local1 >= _local2){
                return (true);
            };
            return (false);
        }
        public static function isAtMaxFranchiseCount():Boolean{
            var _local1:int = getFranchisesCount(Global.player.uid);
            if (_local1 >= maxFranchiseCount){
                return (true);
            };
            return (false);
        }
        public static function get maxFranchiseCount():int{
            return (Global.gameSettings.getInt("maxReceivedFranchises"));
        }
        public static function get maxAllowedFranchiseCount():int{
            return (Math.min(Global.gameSettings.getInt("maxReceivedFranchises"), (maxAllowedFranchiseCountFromNeighbors + Global.player.franchiseUnlocksPurchased)));
        }
        public static function get maxAllowedFranchiseCountFromNeighbors():int{
            return (Math.min(Global.gameSettings.getInt("maxReceivedFranchises"), (Math.floor((Global.player.maxNeighbors / Global.gameSettings.getInt("neighborsPerLotsite"))) + 1)));
        }
        public static function get getNeededNeighborCountForNextFranchise():int{
            var _local1:int;
            _local1 = maxAllowedFranchiseCountFromNeighbors;
            var _local2:int = (_local1 * Global.gameSettings.getInt("neighborsPerLotsite"));
            return ((_local2 - Global.player.maxNeighbors));
        }
        public static function get getNextFranchiseUnlockCost():int{
            return (Global.gameSettings.getInt("franchiseUnlockCost"));
        }
        public static function isLocked(_arg1:Item):Boolean{
            var _local2:Boolean;
            if (Global.player.m_licenses[_arg1.name] == 1){
                return (false);
            };
            if (_arg1.isEmptyLot){
                return (!(lotSiteCheck(_arg1)));
            };
            switch (_arg1.unlock){
                case Item.UNLOCK_LEVEL:
                    _local2 = (Global.player.level < _arg1.requiredLevel);
                    break;
                case Item.UNLOCK_NEIGHBOR:
                    _local2 = !(Global.player.checkNeighbors(_arg1.requiredNeighbors));
                    break;
                case Item.UNLOCK_PERMITS:
                    _local2 = !(ExpansionManager.instance.hasPassedExpansionGate());
                    if (((!(_local2)) && ((_arg1.requiredQuestFlag.length > 0)))){
                        if (!Global.questManager.isFlagReached(_arg1.requiredQuestFlag)){
                            _local2 = true;
                        };
                    };
                    break;
                case Item.UNLOCK_OLD_QUEST_OR_PURCHASE:
                case Item.UNLOCK_QUEST_FLAG:
                    _local2 = !(Global.questManager.isFlagReached(_arg1.requiredQuestFlag));
                    break;
                case Item.UNLOCK_SEEN_FLAG:
                    _local2 = ((!(Global.player.getSeenFlag(_arg1.requiredSeenFlag))) && (!(Global.player.getSeenSessionFlag(_arg1.requiredSeenFlag))));
                    break;
                case Item.UNLOCK_POPULATION:
                    _local2 = !(Global.player.checkPopulation(_arg1.requiredPopulation));
                    break;
                case Item.UNLOCK_QUEST_AND_LEVEL:
                    if (_arg1.requiredQuestFlag.length > 0){
                        _local2 = (((Global.player.level < _arg1.requiredLevel)) || (!(Global.questManager.isFlagReached(_arg1.requiredQuestFlag))));
                    } else {
                        _local2 = (Global.player.level < _arg1.requiredLevel);
                    };
                    break;
                case Item.UNLOCK_QUEST_AND_NEIGHBOR:
                    if (_arg1.requiredQuestFlag.length > 0){
                        _local2 = ((!(Global.questManager.isFlagReached(_arg1.requiredQuestFlag))) || (!(Global.player.checkNeighbors(_arg1.requiredNeighbors))));
                    } else {
                        _local2 = !(Global.player.checkNeighbors(_arg1.requiredNeighbors));
                    };
                    break;
                default:
                    _local2 = (Global.player.level < _arg1.requiredLevel);
            };
            return (_local2);
        }
        public static function shouldShowCantBuyDialog(_arg1:Item):Boolean{
            var _local2:Boolean = (((((((_arg1.unlock == Item.UNLOCK_QUEST_FLAG)) || ((_arg1.unlock == Item.UNLOCK_QUEST_AND_LEVEL)))) || ((_arg1.unlock == Item.UNLOCK_QUEST_AND_NEIGHBOR)))) || ((_arg1.unlock == Item.UNLOCK_SEEN_FLAG)));
            return (((_local2) && (hasUnlockedFlag(_arg1))));
        }
        public static function hasUnlockedFlag(_arg1:Item):Boolean{
            return ((((((_arg1.requiredQuestFlag.length > 0)) && (!(Global.questManager.isFlagReached(_arg1.requiredQuestFlag))))) || ((((((_arg1.requiredSeenFlag.length > 0)) && (!(Global.player.getSeenFlag(_arg1.requiredSeenFlag))))) && (!(Global.player.getSeenSessionFlag(_arg1.requiredSeenFlag)))))));
        }
        public static function getUnlockText(_arg1:Item):Object{
            var _local2:String;
            var _local3:String;
            var _local5:LocalizationObjectToken;
            var _local4:String = ZLoc.t("Dialogs", "RequiresText");
            var _local6 = (_arg1.className == "LotSite");
            if (_local6){
                if (isAtMaxFranchiseCount()){
                    _local4 = ZLoc.t("Dialogs", "ReachedText");
                    _local2 = ZLoc.t("Dialogs", "MaxFranchisesText");
                    return ({
                        line1:_local2,
                        line2:_local3,
                        requires:_local4
                    });
                };
                _local4 = ZLoc.t("Dialogs", "InviteText");
                _local5 = ZLoc.tk("Dialogs", "Neighbor", "", getNeededNeighborCountForNextFranchise);
                _local2 = ZLoc.t("Dialogs", "neighbor_text", {
                    amount:getNeededNeighborCountForNextFranchise,
                    neighbor:_local5
                });
                return ({
                    line1:_local2,
                    line2:_local3,
                    requires:_local4
                });
            };
            switch (_arg1.unlock){
                case Item.UNLOCK_LEVEL:
                    _local2 = ZLoc.t("Dialogs", "level_text", {amount:_arg1.requiredLevel});
                    break;
                case Item.UNLOCK_NEIGHBOR:
                    _local4 = ZLoc.t("Dialogs", "InviteText");
                    _local5 = ZLoc.tk("Dialogs", "Neighbor", "", _arg1.requiredNeighbors);
                    _local2 = ZLoc.t("Dialogs", "neighbor_text", {
                        amount:_arg1.requiredNeighbors,
                        neighbor:_local5
                    });
                    break;
                case Item.UNLOCK_PERMITS:
                    if ((((_arg1.requiredQuestFlag.length > 0)) && (!(Global.questManager.isFlagReached(_arg1.requiredQuestFlag))))){
                        _local2 = ZLoc.t("Dialogs", "quest_text1");
                    } else {
                        _local2 = ExpansionManager.instance.getUnlockLabel();
                    };
                    break;
                case Item.UNLOCK_QUEST_FLAG:
                    _local2 = ZLoc.t("Dialogs", "quest_text1");
                    break;
                case Item.UNLOCK_SEEN_FLAG:
                    _local2 = ZLoc.t("Dialogs", "collection_text1");
                    break;
                case Item.UNLOCK_POPULATION:
                    _local2 = ZLoc.t("Dialogs", "population_text1", {amount2:_arg1.requiredPopulationScaled});
                    break;
                case Item.UNLOCK_QUEST_AND_LEVEL:
                    if ((((_arg1.requiredQuestFlag.length > 0)) && (!(Global.questManager.isFlagReached(_arg1.requiredQuestFlag))))){
                        _local2 = ZLoc.t("Dialogs", "quest_text1");
                    } else {
                        _local2 = ZLoc.t("Dialogs", "level_text", {amount:_arg1.requiredLevel});
                        _local3 = "";
                    };
                    break;
                case Item.UNLOCK_QUEST_AND_NEIGHBOR:
                    if ((((_arg1.requiredQuestFlag.length > 0)) && (!(Global.questManager.isFlagReached(_arg1.requiredQuestFlag))))){
                        _local2 = ZLoc.t("Dialogs", "quest_text1");
                    } else {
                        _local4 = ZLoc.t("Dialogs", "InviteText");
                        _local5 = ZLoc.tk("Dialogs", "Neighbor", "", _arg1.requiredNeighbors);
                        _local2 = ZLoc.t("Dialogs", "neighbor_text", {
                            amount:_arg1.requiredNeighbors,
                            neighbor:_local5
                        });
                    };
                    break;
                case Item.UNLOCK_OLD_QUEST_OR_PURCHASE:
                    _local2 = "";
                    _local3 = "";
                    _local4 = "";
                    break;
            };
            return ({
                line1:_local2,
                line2:_local3,
                requires:_local4
            });
        }

    }
}//package Display.MarketUI 
